/**
 * 
 */
package com.et114.modules.org.services;

import com.et114.components.acegi.Constants;
import com.et114.core.common.CONST;
import com.et114.core.exception.CheckException;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.core.utility.StringPool;
import com.et114.modules.org.dao.TitleDAO;
import com.et114.modules.org.vo.RoleVo;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.TitleRoleRelVo;

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;

import net.sf.ehcache.CacheException;

/**
 * @author guanhw
 */
public class TitleManager extends BaseServicesImpl {
	private TitleDAO titleDAO ;
	public void setTitleDAO ( TitleDAO titleDAO ) {
		this.titleDAO = titleDAO;
	} 
	
	public List getAllTitlesRoles() throws CheckException {
		List titles = titleDAO.getTitles() ;
		for ( int i = 0 ; i < titles.size() ; i ++  ) {
			TitleMsgVo title = ( TitleMsgVo ) titles.get( i ) ;
			List roles = titleDAO.getRolesByTitle(  title  ) ;
			title.setRoles( roles ) ;
		}
		return titles ;
	}
	
	public TitleMsgVo getTitle( Long titleId ) throws CheckException {
		TitleMsgVo vo = new TitleMsgVo();vo.setTitleId ( titleId ) ; 
		return titleDAO.getTitle( vo ) ;
	}
	
	
	public List getFunctionsByTitle( Long titleId )  throws CheckException {
		return titleDAO.getFunctionsByTitle ( titleId ) ;
	}

	public List getRolesByTitle ( TitleMsgVo vo , byte page ) throws CheckException {
		if( page == CONST.PAGE ) return titleDAO.getRolesByTitle ( vo ) ;
		return getRolesByTitleNPage( vo ) ;
	}
	
	public Long countgetRolesByTitle( TitleMsgVo vo ) throws CheckException { 
		return titleDAO.countgetRolesByTitle ( vo ) ; 
	}
	
	public List getRolesByTitleNPage ( TitleMsgVo vo ) throws CheckException {
		return titleDAO.getRolesByTitleNPage ( vo ) ;
	}
	
	
	public List getRolesByTitle ( Long titleId ) throws CheckException {
		return titleDAO.getRolesByTitle ( titleId ) ;
	}	

	
	
	public void deleteTitleRoleRel( Long titleId , Long[] roleIds , Long operId ) throws CheckException , CacheException {
		if ( roleIds == null ) return ;
		for ( int i = 0; i < roleIds.length ; i ++  ){
			Long deleteId = roleIds[ i ] ; 
			TitleRoleRelVo titleRoleRel = new TitleRoleRelVo() ;
			titleRoleRel.setRoleId ( deleteId ) ;
			titleRoleRel.setTitleId ( titleId  ) ; 
			titleRoleRel.setOperUser ( operId );
			titleDAO.deleteTitleRoleRel ( titleRoleRel ) ;
		}
	
		if ( roleIds != null  )
			OrgFactory.getGroupManager ( ).titleRoleRelmodifyInCache ( titleId , roleIds , Constants.CACHE_DEL ) ;
	}
	
	public List queryTitlesOfGroupPage( TitleMsgVo titleMsgVo  ) throws CheckException {   
		return titleDAO.queryTitlesOfGroupPage ( titleMsgVo ) ; 
	}
	
	public Long countqueryTitlesOfGroup( Long groupId ) throws CheckException {   
		return titleDAO.countqueryTitlesOfGroup ( groupId ) ; 
	}

	
	public void saveTitleRoleRel( TitleMsgVo title , String principalIds  ) throws CheckException  , CacheException {
		Long titleId = title.getTitleId ( ) ; 
		List roles = new ArrayList( ) ; 
		List tmpRoles = new java.util.ArrayList();
		if ( principalIds != null ) {
			roles = getRolesByTitle( title , CONST.NPAGE ) ;
			principalIds = principalIds.trim ( ) ; 
			StringTokenizer st = new StringTokenizer( principalIds , StringPool.SEMICOLON ) ;
			int count = st.countTokens ( ) ;
			for ( int i = 0 ; i < count ; i ++  ) {
				String nt = st.nextToken ( ) ;
				//check
				boolean mark = false ;
				if ( roles.size() == 0  )  mark = true;
				for ( Iterator iter = roles.iterator ( ); iter.hasNext ( ) ;  ) {
					RoleVo role = ( RoleVo )  iter.next ( );
					Long roleId = role.getRoleId ( );
					if ( roleId .toString ( ).equals ( nt ) ) {
						iter.remove ( );
						mark = false ;
						break;
					}
					else mark = true ;
				}
				if ( mark  ) tmpRoles.add ( nt ) ;
			}	
			
				int i = 0 ; 
			    Long[ ] roleIds = new Long[ tmpRoles.size ( ) ] ;
				for ( Iterator iter = tmpRoles.iterator ( ); iter.hasNext ( ) ;  ) {
					Long principalId = Long.valueOf ( iter.next ( ).toString ( ) ) ;
					roleIds[ i ] =  principalId ;
					TitleRoleRelVo titleRoleRel = new TitleRoleRelVo() ;
					titleRoleRel.setRoleId ( principalId ) ;
					titleRoleRel.setTitleId ( titleId ) ; 
					titleRoleRel.setCrtUser ( title.getOperUser ( ) ) ; 
					titleDAO.createTitleRoleRel ( titleRoleRel ) ; 
					i ++ ; 
				}	
				
				//cache
				if ( tmpRoles .size ( ) > 0  ) {
					OrgFactory.getGroupManager ( ).titleRoleRelmodifyInCache (
							titleId , roleIds , Constants.CACHE_NEW );
				}
				
				for ( Iterator iter = roles.iterator ( ); iter.hasNext ( ) ;  ) {
					RoleVo role = ( RoleVo )  iter.next ( );
					Long principalId = role.getRoleId ( );
					TitleRoleRelVo titleRoleRel = new TitleRoleRelVo() ;
					titleRoleRel.setRoleId ( principalId ) ;
					titleRoleRel.setTitleId ( titleId ) ; 
					titleRoleRel.setOperUser ( title.getOperUser ( ) );
					titleDAO.deleteTitleRoleRel ( titleRoleRel ) ;
				}
				
				//cache
				if ( roles .size ( ) > 0  ) {
					OrgFactory.getGroupManager ( ).titleRoleRelmodifyInCache (
							titleId , roles , Constants.CACHE_DEL );
				}
				/* since 1.11
				if ( roles.size ( ) > 0 || tmpRoles.size ( ) > 0 ) {
					OrgFactory.getGroupManager ( ).modifyInCache ( titleId ) ;
				}*/
					
		}
	}
}
